#include "view.h"

#include "columnlist.h"
#include "querytree.h"
#include "simplequery.h"
#include "schemacomponentformula.h"

#include <stdio.h>
using namespace std;

View::View(ColumnList * columns, QueryTree * query, const string & name):SchemaComponent(name)
{
    if(columns == NULL) this->columns = NULL;
    else this->columns = columns->clone();

    if(query == NULL) this->query = NULL;
    else this->query = query->clone();
}

View::~View()
{
    delete columns; columns = NULL;     // NULL;
    delete query; query = NULL;         // NULL;
}

View::View(const View& other):SchemaComponent(other.name)
{
    if (other.columns!=NULL) columns = other.columns->clone();
	else columns=NULL;
    if (other.query!=NULL) query = other.query->clone();
	else query=NULL;
}

QueryTree * View::getQueryTree() const
{
    return query;
}

ColumnList * View::getColumnList() const
{
    return columns;
}

void View::setQueryTree(QueryTree * query)
{
    delete this->query; this->query = NULL;
    this->query = query->clone();
}

void View::setColumnList(ColumnList * columns)
{
    delete this->columns; this->columns = NULL;
    this->columns = columns->clone();
}

void View::addColumn(Column * column)
{
    if(columns == NULL) columns = new ColumnList();
    if(column != NULL) columns->addColumn(column);
}

void View::addColumn(const string & name)
{
    if(columns == NULL) columns = new ColumnList();
    columns->addColumn(name);
}

string View::toString()
{
    string str = "VIEW: " + name + "\n";

    str = str + "COLUMNS:\n";
    if(columns != NULL){   // if not NULL
        str = str + columns->toString() + "\n";
    }//if

    if(query != NULL){
        str = str + "QUERY:\n" + query->toString() + "\n";
    }//if

    return str;
}

ColumnList * View::getColumns(){
	return columns;
}

SchemaComponent * View::clone()
{
    return new View(*this);
}
